// pages/search0/search0.js
const util = require('../../utils/util.js')
import Bmob from '../../utils/dist/Bmob-1.7.0.min.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal:false,
    showModal:false,
    showModal2:false,
    showModal3:false,
    showModal4:false,
    showModal5:false,


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var xqsj = []
    var shuju = JSON.parse(options.shuju);
    // console.log(options.shuju,"45")
    //var shuju = options.shuju
    this.setData({
      shuju: shuju
    })
    var imgPic = []
    var imgPic1 = []
    var content2 = []
    for (var i = 0; i < 1; i++) {
      imgPic1.push(shuju.pic)
    }
    for (var i = 0; i < shuju.process.length; i++) {
      imgPic.push(shuju.process[i].pic)
      content2.push(shuju.process[i].pcontent.replace("<br />", ""))

    }
    this.setData({
      content2: content2
    })
    this.setData({
      imgPic1: imgPic1
    })
    this.setData({
      imgPic: imgPic
    })

    
    console.log(shuju,555)
   /*  var that = this
    console.log(options.id)
    wx.request({
      url: 'https://jisusrecipe.market.alicloudapi.com/recipe/detail?id=' + options.id, // 仅为示例，并非真实的接口地址
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json', // 默认值
        "Authorization": "APPCODE 3930a9e0791146739618fd11f8ec15d4"
      },
      success(res) {
        console.log(res.data)
        console.log(res.data,"原始数据")
        that.setData({
          xqsj: res.data
        })

        console.log(res.data.result.process,"数组")
        var content1 = []
        var content1 = res.data.result.process
        var imgPic = []
        var imgPic1 = []
        var source = ''
        var content2 = []
        for (var i = 0; i < 1; i++) {
          imgPic1.push(res.data.result.pic)
        }
        for (var i = 0; i < res.data.result.process.length; i++) {
          imgPic.push(res.data.result.process[i].pic)
          content2.push(content1[i].pcontent.replace("<br />", ""))
          
        }
        that.setData({
          content2:content2
        })
        that.setData({
          imgPic1: imgPic1
        })
        that.setData({
          imgPic: imgPic
        })
        that.setData({
          source: res.data.result.content
        })
        var result1 = that.removeChars(source, '<br />')
        console.log(result1)
        that.setData({
          content: result1
        })
        console.log(that.data.source)
        console.log(imgPic)
      }
    })
    that.setData({
      id:options.id
    })
      */
  },

  removeChars: function (source, chars) {            //正则去掉<br />符号
    console.log(this.data.source)
    var reg = new RegExp(chars, 'gi');
    var result1 = this.data.source.replace(reg, "");
    return result1;
  },


  previewImage: function (e) {   //图片显示
    wx.previewImage({
      current: e.target.dataset.src,
      urls: this.data.imgPic
    })
  },

  previewImage1: function (e) {    //图片显示
    wx.previewImage({
      current: e.target.dataset.src,
      urls: this.data.imgPic1
    })
  },

  funSave: function(){    //保存
 
    let current = Bmob.User.current();
    const query = Bmob.Query('_User');
    query.select("savenumber");
    query.get(current.objectId).then(res => {
    console.log(res)
    this.setData({
      savenumber: res.savenumber,
    })
    if (this.data.savenumber >= 5) {
      console.log("对不起您已经有5条的保存了")
      this.setData({
        showModal3:true,
        showModal5:true,
      })
    } else {

      const query = Bmob.Query('savedata');
      query.equalTo("who", "==", current.objectId);    //检查我的菜单里是否有发布过这条菜单
      query.select("dataid");      //单条数据查询，用id来识别是否有此条菜单
      query.find().then(res => {
        console.log(res,"5")
        var dataids=[]
        for(var i=0;i<res.length;i++){    //提取res里的dataid数值到dataids数组里
          dataids.push(        
            res[i].dataid
          )
        }
        //console.log(typeof this.data.id)
        var judge = dataids.indexOf(Number(this.data.id))    //判断dataids数组里是否有当前菜品的id,如果有则judge为返回下标，无则返回-1
        if(judge==-1){      //用户没有保存过，所以执行保存代码
          console.log(judge, "78")
          console.log(dataids)
          const pointer = Bmob.Pointer('_User')      //数据关联
          const poiID = pointer.set(current.objectId)

          query.set("who", poiID) //数据关联
          query.set("dataid", Number(this.data.shuju.id))  //Number为将this.data.id转换为Number型
          //console.log(typeof this.data.id)
          //console.log(typeof current.objectId)
          query.set("pic", this.data.imgPic1)
          query.set("name", this.data.shuju.name)
          query.set("time", this.data.shuju.cookingtime)
          query.set("people", this.data.shuju.peoplenum)
          query.save().then(res => {
            console.log(res)
            const query = Bmob.Query('_User')
            //console.log(current.objectId,"1")        //计数，每当用户点击一次保存则增加一次
            query.get(current.objectId).then(res => {
              res.increment('savenumber',1)
              res.save()

              wx.showToast({          //保存成功提示图
                title: '保存成功',
                icon: 'success',
                duration: 3000
              });

            }).catch(err => {
              console.log(err)
            })
         }).catch(err => {
            console.log(err)
          })
        }else{
          console.log("你已经保存过了！")
          this.setData({
            showModal: true,
            showModal2:true,
          })
          

        }
         
      });
    }
    console.log(this.data.savenumber)
  }).catch(err => {
    console.log(err)
  })
},

  funLike: function(){     //推荐

    let current = Bmob.User.current();
    const query = Bmob.Query('_User');
    query.select("recommendednumber");
    query.get(current.objectId).then(res => {
      console.log(res)
      this.setData({
        recommendednumber: res.recommendednumber,
      })
      if (this.data.recommendednumber >=5) {
        console.log("对不起您已经有5条的推荐了")
        this.setData({
          showModal3:true,
          showModal4:true
        })
      } else {

        const query = Bmob.Query('recommendeddata');
        query.equalTo("who", "==", current.objectId);    //检查我的菜单里是否有发布过这条菜单
        query.select("dataid");      //单条数据查询，用id来识别是否有此条菜单
        query.find().then(res => {
          console.log(res, "5")
          var dataids = []
          for (var i = 0; i < res.length; i++) {    //提取res里的dataid数值到dataids数组里
            dataids.push(
              res[i].dataid
            )
          }
          //console.log(typeof this.data.id)
          var judge = dataids.indexOf(Number(this.data.id))    //判断dataids数组里是否有当前菜品的id,如果有则judge为返回下标，无则返回-1
          if (judge == -1) {      //用户没有保存过，所以执行保存代码
            console.log(judge, "78")
            console.log(dataids)
            const pointer = Bmob.Pointer('_User')      //数据关联
            const poiID = pointer.set(current.objectId)

            query.set("who", poiID) //数据关联
            query.set("dataid", Number(this.data.shuju.id))  //Number为将this.data.id转换为Number型
            //console.log(typeof this.data.id)
            //console.log(typeof current.objectId)
            query.set("classid", this.data.shuju.classid)
            query.set("content", this.data.shuju.content)
            query.set("material", this.data.shuju.material)
            query.set("process", this.data.shuju.process)
            query.set("tag", this.data.shuju.tag)
            query.set("pic", this.data.imgPic1)
            query.set("name", this.data.shuju.name)
            query.set("time", this.data.shuju.cookingtime)
            query.set("people", this.data.shuju.peoplenum)
            query.save().then(res => {
              console.log(res)
              const query = Bmob.Query('_User')
              //console.log(current.objectId,"1")        //计数，每当用户点击一次保存则增加一次
              query.get(current.objectId).then(res => {
                res.increment('recommendednumber')
                res.save()

                wx.showToast({      //推荐成功提示图
                  title: '推荐成功',
                  icon: 'success',
                  duration: 3000
                });

              }).catch(err => {
                console.log(err)
              })
            }).catch(err => {
              console.log(err)
            })
          } else {
            console.log("你已经推荐过了！")
            this.setData({
              showModal: true,
               showModal1: true,
            })
           
          }

        });
      }
      console.log(this.data.savenumber)
    }).catch(err => {
      console.log(err)
    })

  },
  // 弹出层里面的弹窗
  ok: function () {
    this.setData({
      showModal: false,
      showModal1:false,
      showModal2:false,
    })
  },
  ok1:function(){
    this.setData({
      showModal3:false,
      showModal4:false,
      showModal5:false,
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})